<?php
require_once 'lib/BaseModel.php';

class Bookmark extends Tamtay_Table_Abstract
{
    protected $_name = 'mytt_bookmark';
    protected $_primary = 'bid';
    
    public function addBookmark($data)
    {
        $this->insert($data);
        return $this->getDefaultAdapter()->lastInsertId();
    }
        
    public function getBookmark($bid)
    {
        $db = $this->getSlaveDB();
        $sql = 'SELECT * FROM mytt_bookmark WHERE bid = ?';
        return $db->fetchRow($sql, $bid); 
    }
    
    public function getBookmarks($page = 1, $title = '', $cid = 0, $uid = 0, $isMine = FALSE, $isFriend = FALSE)
    {
        $offset = ($page - 1) * BOOKMARKS_PER_PAGE;
        $db = $this->getSlaveDB();
        $where = '';
        if ($title != '') {
            $clause = "title LIKE '%" . $title . "%'";
            $where = $this->_makeWhere($clause, $where);
        }   
        if ($cid != '' AND $cid != 0){
            $clause = "cid = '" . $cid . "'";
            $where = $this->_makeWhere($clause, $where);
        }    
        if ($uid != 0){
            $clause = "uid = '" . $uid . "'";
            $where = $this->_makeWhere($clause, $where);
            if ( ! $isMine){
                if ($isFriend){
                    $clause = "(privacy = '" . BOOKMARK_PRIVACY_FRIEND . "' OR privacy = '" . BOOKMARK_PRIVACY_PUBLIC . "')";   
                } else {
                    $clause = "privacy = '" . BOOKMARK_PRIVACY_PUBLIC . "'";   
                }
                $where = $this->_makeWhere($clause, $where);
            }
        } else {
            $clause = "privacy = '" . BOOKMARK_PRIVACY_PUBLIC . "'";             
            $where = $this->_makeWhere($clause, $where);
        }                                                                                    
        $sql = 'SELECT * FROM mytt_bookmark ' . $where . ' ORDER BY changed DESC LIMIT ' . BOOKMARKS_PER_PAGE . ' OFFSET ' . $offset;
        return $db->fetchAll($sql);    
    }
    
    private function _makeWhere($clause, $where, $prefix = ' AND ')
    {
        if ($where == ''){
            $prefix = ' WHERE ';    
        }
        $where .= $prefix . $clause;
        return $where;   
    }
    
    public function getBookmarksCount($title = '', $cid = 0, $uid = 0, $isMine = FALSE, $isFriend = FALSE) {
        $db = $this->getSlaveDB();
        $where = '';
        if ($title != '') {
            $clause =  "title LIKE '%" . $title . "%' ";
            $where = $this->_makeWhere($clause, $where);
        }
        if ($cid != 0){
            $clause = "cid = '" . $cid . "'";
            $where = $this->_makeWhere($clause, $where);
        }    
        if ($uid != 0){
            $clause = "uid = '" . $uid . "'";
            $where = $this->_makeWhere($clause, $where);
            if ( ! $isMine){
                if ($isFriend){
                    $clause = "(privacy = '" . BOOKMARK_PRIVACY_FRIEND . "' OR privacy = '" . BOOKMARK_PRIVACY_PUBLIC . "')";   
                } else {
                    $clause = "privacy = '" . BOOKMARK_PRIVACY_PUBLIC . "'";   
                }
                $where = $this->_makeWhere($clause, $where);
            }
        } else {
            $clause = "privacy = '" . BOOKMARK_PRIVACY_PUBLIC . "'";
            $where = $this->_makeWhere($clause, $where);
        }  
        $sql = 'SELECT COUNT(*) FROM mytt_bookmark' . $where;
        return $db->fetchOne($sql);
    }
    
    public function updateBookmark($data, $bid)
    {
        //$where = $this->getDefaultAdapter->quoteInto('bid = ?', $bid);
        $where = "bid = '$bid'";
        $this->update($data, $where);
    }
    
    public function deleteBookmark($bid, $ref = NULL, $type = NULL)
    {
        $db = $this->getMasterDB();
        $where =  'WHERE bid = ?'; 
        if ($ref != NULL){
            $where .= " AND ref = '$ref'";    
        }        
        if ($type != NULL){
            $where .= " AND type = '$type'";
        }
        $sql = 'DELETE FROM mytt_bookmark ' . $where;
        $db->query($sql, $bid);
    }   
    
    public function getUserBookmarks($uid, $page = 1, $perPage = BOOKMARKS_PER_PAGE)
    {
        $db = $this->getSlaveDB();
        $offset = ($page - 1) * $perPage;
        $db = $this->getSlaveDB();
        $sql = 'SELECT * FROM mytt_bookmark WHERE uid = ? ORDER BY created DESC LIMIT ' . $perPage . ' OFFSET ' . $offset;
        return $db->fetchAll($sql, $uid);
    }
    
    public function getUserBookmarkCount($uid)
    {
        $db = $this->getSlaveDB();
        $sql = 'SELECT COUNT(*) FROM mytt_bookmark WHERE uid = ?';
        return $db->fetchOne($sql, $uid);
    }
    
    public function getBookmarksByType($ref, $type, $page, $perPage = BOOKMARKS_PER_PAGE)
    {
        $offset = ($page - 1) * $perPage;
        $db = $this->getSlaveDB();        
        $sql = 'SELECT * FROM mytt_bookmark WHERE ref = ? AND type = ? AND privacy = ? ORDER BY changed, created DESC LIMIT ' . $perPage . ' OFFSET ' . $offset;
        return $db->fetchAll($sql, array($ref, $type, BOOKMARK_PRIVACY_PUBLIC));    
    }
    
    public function countBookmarkByType($ref, $type)
    {
        $db = $this->getSlaveDB();              
        $sql = 'SELECT COUNT(*) FROM mytt_bookmark WHERE ref = ? AND type = ? AND privacy = ?';
        return $db->fetchOne($sql, array($ref, $type, BOOKMARK_PRIVACY_PUBLIC));
    }
    
    public function getUsersBookmarks($uids, $page, $perPage = BOOKMARKS_PER_PAGE)
    {
        $offset = ($page - 1) * $perPage;
        $db = $this->getSlaveDB();
        $in = $this->_makeIn($uids);
        if ($in == ''){
            return FALSE;
        }
        $sql = "SELECT * FROM mytt_bookmark WHERE privacy = '" . BOOKMARK_PRIVACY_PUBLIC . "' AND uid IN " . $in . "ORDER BY changed DESC LIMIT " . $perPage . " OFFSET " . $offset;
        return $db->fetchAll($sql);
    }
    
    public function getUsersBookmarkCount($uids)
    {
        $db = $this->getSlaveDB();
        $in = $this->_makeIn($uids);
        if ($in == ''){
            return FALSE;
        }
        $sql = "SELECT COUNT(*) FROM mytt_bookmark WHERE privacy = '" . BOOKMARK_PRIVACY_PUBLIC . "' AND uid IN " . $in;
        return $db->fetchOne($sql);
    }
    
    private function _makeIn($uids)
    {
        $in = ''; 
        for ($i = 0; $i < count($uids); $i++){
            if ($in != ''){
                $in .= ',' . $uids[$i]->uid;
            } else {
                $in .= $uids[$i]->uid;
            }
        }
        if ($in != ''){
            $in = '(' . $in . ')';
        }
        return $in;
    }
}

